package com.white.hot.controller.wx;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.white.hot.constant.ConstantInfo;
import com.white.hot.dto.resp.BaseResp;
import com.white.hot.dto.resp.MapInfo;
import com.white.hot.dto.resp.WXAccessTokenResp;
import com.white.hot.utils.TextUtils;

@Controller
@RequestMapping("/wxlogin")
public class WXLoginController
{

	private final String wxTokenReq = "https://api.weixin.qq.com/sns/oauth2/access_token?";
	private String appid = "wx0fa3e6f18744566e";
	private String secret = "16b9fabcc5ffccb54adeda8e53822933";

	@RequestMapping("/getAccessToken")
	@ResponseBody
	private Object getAccessToken(@RequestParam(value = "code") String code)
	{
		BaseResp br = new BaseResp();
		if (TextUtils.isEmpty(code))
		{
			br.setStatus(ConstantInfo.RSC_PARAM_INCOMPLETE);
			br.setInfo(ConstantInfo.RSS_PARAM_INCOMPLETE);
			return br;
		} else
		{
			String url = wxTokenReq + "appid=" + appid + "&secret=" + secret
					+ "&code=" + code + "&grant_type=authorization_code";
			URI uri = URI.create(url);
			HttpClient client = new DefaultHttpClient();
			HttpGet get = new HttpGet(uri);
			HttpResponse response;
			try
			{
				response = client.execute(get);
				if (response.getStatusLine().getStatusCode() == 200)
				{
					HttpEntity entity = response.getEntity();

					BufferedReader reader = new BufferedReader(
							new InputStreamReader(entity.getContent(), "UTF-8"));
					StringBuilder sb = new StringBuilder();

					for (String temp = reader.readLine(); temp != null; temp = reader.readLine())
					{
						sb.append(temp);
					}

					reader.close();

					JSONObject object = JSONObject.parseObject(sb.toString().trim());
					MapInfo mi = new MapInfo();
					WXAccessTokenResp wr = new WXAccessTokenResp();
					if (object.getIntValue("errcode") != 0)
					{
						wr.errcode = object.getIntValue("errcode");
						wr.errmsg = object.getString("errmsg");
						mi.setWxAccessTokenResp(wr);
						br.setMapInfo(mi);
						return br;
					}
					wr.accessToken = object.getString("access_token");
					wr.openID = object.getString("openid");
					wr.refreshToken = object.getString("refresh_token");
					wr.expires_in = object.getLong("expires_in");
					mi.setWxAccessTokenResp(wr);
					br.setMapInfo(mi);
				}
			} catch (ClientProtocolException e)
			{
				e.printStackTrace();
				br.setStatus(ConstantInfo.RSC_INNER_ERROR);
				br.setInfo(ConstantInfo.RSS_INNER_ERROR);
			} catch (IOException e)
			{
				e.printStackTrace();
				br.setStatus(ConstantInfo.RSC_INNER_ERROR);
				br.setInfo(ConstantInfo.RSS_INNER_ERROR);
			} catch (IllegalStateException e)
			{
				e.printStackTrace();
				br.setStatus(ConstantInfo.RSC_INNER_ERROR);
				br.setInfo(ConstantInfo.RSS_INNER_ERROR);
			} catch (JSONException e)
			{
				e.printStackTrace();
				br.setStatus(ConstantInfo.RSC_INNER_ERROR);
				br.setInfo(ConstantInfo.RSS_INNER_ERROR);
			}

			return br;
		}
	}
}
